<?php
namespace Shopmodule\Controller;
use Think\Controller;
class UserController extends ExtendController {
	public function index(){
		$wl = C("WL");
		$usernick = I("usernick");
		$usernick = urldecode_utf8($usernick);
		$page = I("page")?I("page"):0;
		$G = D("Shopadmin/UsersGifts");
		$G->sid=$this->sid;
		$G->usernick=$usernick;
		$G->limit=($page*7).",7";
		$G->where="sm_events_gifts.type<>7";
		$G->field=" `sm_users_gifts`.`id`,
				 `sm_events_gifts`.`type`,
				 `sm_events_gifts`.`level`,
				 `sm_events_gifts`.`price`,
				 `sm_events_gifts`.`name`,
				 `sm_events_gifts`.`num_iids`,
				 `sm_users_gifts`.`actiontime`,
				 `sm_users_gifts`.`wlcode`,
				 `sm_users_gifts`.`wlid`,
				 `sm_users_gifts`.`kamiid`,
				 `sm_shop_users`.`addtime`
				 ";//`sm_users_gifts`.`id`,
		$winner = $G->SendList();
		$G->GetTotal();
		foreach($winner as $k=>$win){
			$win["wlcode"]=$wl[$win["wlcode"]];
			if($win["type"]==4 && isset($win["kamiid"]) ){
				$KM = M("EventsGiftsKm");
				$km = current($KM->where("`id`=".$win["kamiid"])->getField("id,cardno,secret"));
				$win["cardno"]=$km["cardno"];
				$win["secret"]=$km["secret"];
			};
			$w[]=$win;
		}
		$data["data"]=$w;
		$data["count"]=$G->total;
		$this->ajaxReturn($data);
	}
	public function collect(){
		$usernick = I("usernick");
		$sid = I("sid");
		$usernick = urldecode_utf8($usernick);
		$UZUser = M("User");
		$uzuid = $UZUser->where("`usernick`='".$usernick."'")->getField("`id`");
		$SHOPUser = M("ShopUsers");
		$shopuid = $SHOPUser->where("`userid`=".$uzuid." AND `sid`=".$sid)->getField("id");
		$EUser = M("EventsUsers");
		$data["collect"]=1;
		$re["status"]=$EUser->where("`uzuid`=".$shopuid)->save($data);
		$this->ajaxReturn($re);
	}
	public function brandme(){
		$usernick = I("usernick");
		$brandid = I("brandid");
		$usernick = urldecode_utf8($usernick);
		$UZUser = M("User");
		$uzuid = $UZUser->where("`usernick`='".$usernick."'")->getField("`id`");
		$SHOPUser = M("ShopUsers");
		$shopuid = $SHOPUser->where("`userid`=".$uzuid." AND `sid`=".$sid)->getField("id");
		$EUser = M("EventsUsers");
		$data["refond"]=1;
		$re["status"]=$EUser->where("`uzuid`=".$shopuid)->save($data);
		$this->ajaxReturn($re);
	}
	public function addcart(){
		$usernick = I("usernick");
		$ts = I("get.ts");
		$sign = I("get.sign");
		$top_sign = I("get.top_sign");
		$itemstr = I("get.itemstr");
		$shop_id = I("get.shop_id");
		if($usernick && md5($shop_id."|".$itemstr."|".$usernick."|".$top_sign."|".C("FRONT_SECRET").$ts)==$sign){
			$usernick = urldecode_utf8($usernick);
			$User = M("User");
			$this->UserUZID = $User->where("`usernick`='".$usernick."'")->getField("id");
			if(!$this->UserUZID){
				$data = array();
				$data["usernick"]=$usernick;
				$data["jointime"]=date("Y-m-d H:i:s",time());
				$this->UserUZID = $User->add($data);
			}
			$ShopUser = M("ShopUsers");
			$this->UserSPID = $ShopUser->where("`userid`=".$this->UserUZID." AND `sid`=".$shop_id)->getField("id");
			if(!$this->UserSPID){
				$data = array();
				$data["sid"]=$shop_id;
				$data["userid"]=$this->UserUZID;
				$data["addtime"]=date("Y-m-d H:i:s",time());
				$this->UserSPID = $ShopUser->add($data);
			}
			$SP = M("ShopCfg");
			$cfg = current($SP->where("`sid`=".$shop_id)->getField("id,cart_items,tms_cartadd,tms_cartadd_limit,tms_newcomer"));
			$cart_items_incart = $itemstr?explode(",",$itemstr):false; //购物车中现有的商品
			$cart_items_incfg = $cfg["cart_items"]?explode(",",$cfg["cart_items"]):false; //店铺配置中允许的商品
			$DB = M("EventsUsersExpand");
			$data_expand = $DB->where("uzuid = ".$this->UserSPID)->limit(1)->getField("expand");
			if(!$data_expand){
				if($cfg["cart_items"]){
					//在有限定的情况下
					$item_add = array_intersect($cart_items_incfg,$cart_items_incart);//存储购物车和店铺配置的交集为允许的
				}else{
					$item_add = $cart_items_incart;
				}
				if($item_add){
					$expand["cart_items"]= implode(",",$item_add); 
					unset($data);
					$data["expand"] = json_encode($expand);
					$data["sid"]=$shop_id;
					$data["uzuid"]=$this->UserSPID;
					$DB->add($data);
				}
				$items = array();
			}else{
				$data_expand=json_decode($data_expand,true);
				$items = explode(",",$data_expand["cart_items"]);
				$item_new_incart = array_diff($cart_items_incart,$items); //购物车中和已存储的商品的差集，为新加入的
				if($cfg["cart_items"]){
					$item_add = array_diff(array_intersect($cart_items_incfg,$item_new_incart),$items); //新加入的商品和配置的交集，再与已加入商品的差集为允许的
				}else{
					$item_add = array_diff($item_new_incart,$items); //新加入的商品和已加入商品的差集为允许的
				}
				if(count($item_add)>0){
					$itemsave = array_merge($items,$item_add);//已有的和允许的和集存储起来
					// $dasave["expand"]=implode(",",$itemsave);
					$expand["cart_items"]= implode(",",$itemsave);  //以json形式保存数据
					$dasave["expand"] = json_encode($expand);
					$DB->where("`uzuid`=".$this->UserSPID." AND `sid`=".$shop_id)->save($dasave);
				}
			}
			if($item_add && count($item_add)>0){
				$score_add = (count($item_add)*$cfg["tms_cartadd"]);
				$score_added = (count($items)*$cfg["tms_cartadd"]);
				if($score_added >=$cfg["tms_cartadd_limit"]){
					$score_add = 0;
					$msg = "亲，您获得的次数已超上限哦。请努力购物吧！";
				}else{
					if(($score_add + $score_added)>$cfg["tms_cartadd_limit"]){
						$score_add = $cfg["tms_cartadd_limit"]-$score_added;
					}
				}
				// $reda["msg"]="亲，您已经把".($cfg["cart_items"]?"参与活动的".count($cart_items_incfg)."款商品中的":"")."".count($item_add)."款商品加入购物车，其中".count($item_add)."件新加入的商品共获得了".$score_add."次抽奖机会，可以点击抽奖按钮抽奖啦！";
				$reda["msg"]=$msg?$msg:"亲，新加入购物车的".count($item_add)."件商品共获得了".$score_add."次抽奖机会，可以点击抽奖按钮抽奖啦！";
				$U = D("EventsUsers");
				$Uscore = current($U->where("`uzuid`='".$this->UserSPID."' AND `sid`=".$this->sid." AND `eventid`=0")->limit(1)->getField("id,id,score,likeme,shareme,collect,followme,refond,addtime"));//echo $U->_sql();
				if($Uscore===false){
					unset($data);
					$data["score"] = $cfg["tms_newcomer"]+$score_add;
					$data["uzuid"] = $this->UserSPID;
					$data["sid"] = $this->sid;
					$data["addtime"]=date("Y-m-d H:i:s",time());
					$data["likeme"]=0;
					$data["shareme"]=0;
					$data["collect"]=0;
					$data["followme"]=0;
					$this->scoreid = $U->add($data);
				}else{
					$U->where("uzuid = ".$this->UserSPID)->setInc('score',$score_add);
				}
				// if($cfg["cart_items"]){
					// $cart_items = explode(",",$cfg["cart_items"]);
					// $allow_items = array_intersect($cart_items,$item_add);//检查交集
					// $score_add = (count($allow_items)*$cfg["tms_cartadd"]);
				// }else{
					// $score_add = (count($item_add)*$cfg["tms_cartadd"]);
				// }
			}else{
				if(count($item_new_incart)>0){
					$reda["msg"]="亲，您没有获得抽奖机会，重复加入只算一次哦！请仔细查看抽奖规则，只有将指定商品加入购物车才可以抽奖哦！";
				}else{
					$reda["msg"]="亲，您没有新加入购物车的商品哦！请赶紧去选购吧，每加入一款可抽奖".$cfg["tms_cartadd"]."次哦！";
				}
			}
			$reda["status"]=true;
			$reda["item_add"]=$item_add;
			$reda["score_add"]=$score_add;
		}else{
			$reda["status"]=false;
			$reda["itemstr"]=$itemstr;
		}
		$this->ajaxReturn($reda);
	}
	public function getrange(){
		// 获取游戏排行榜
		$sid = I("sid");
		$curtool = I("curtool");
		// $SCORE = M("GameScore");
		$SP = M("ShopCfg");
		$RANGE = D("RangeTime");
		$range_time = $SP->where("`sid`=".$sid)->getField("`range_time`");
		$ts = $RANGE->getrangetime($range_time,time());
		// $data["data"]=$SCORE->where("`time_round`='".$ts."' AND `game_name`='".$curtool."'")->limit(10)->order("score DESC")->getField("level,range_level,lotts,score",true,true);//取出分数、等级、获得抽奖次数
		$VSC = D("ScoreView");
		$reda = $VSC->where("`time_round`='".$ts."' AND `game_name`='".$curtool."'")->limit(10)->order("score DESC")->getField("level,range_level,lotts,score,usernick",true,true);//取出分数、等级、获得抽奖次数
		foreach($reda as $k=>$v){
			if($reda[$k]["usernick"]){
				$reda[$k]["usernick"]=substr_cut($reda[$k]["usernick"],3);
			}else{
				$reda[$k]["usernick"]="网友...";
			}
		}
		$data["data"]=$reda;
		$data["status"]=1;
		$data["sql"]=$VSC->_sql();
		$this->ajaxReturn($data);
	}
	
}
